package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.ToolLifeListEntity;

import java.util.List;

/**
 * FileName: ToolLifeListAdapter
 * Author: XJH
 * Date: 2021/8/4 15:23
 * Description:
 * History:
 */
public class ToolLifeListAdapter extends BaseQuickAdapter<ToolLifeListEntity.ToolLifeEntity, BaseViewHolder> {

    public ToolLifeListAdapter(@Nullable List<ToolLifeListEntity.ToolLifeEntity> data) {
        super(R.layout.item_tool_life_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ToolLifeListEntity.ToolLifeEntity item) {
            helper.setText(R.id.tvName,item.getEAPPARAM())
                    .setText(R.id.tvLot,item.getTOOLID())
                    .setText(R.id.tvStep,item.getStationName())
                    .setText(R.id.tvCurrentLife,String.valueOf(item.getLIMITLIFE()));

            setEditListener(helper.getView(R.id.etLife),item);
    }

    private void setEditListener(EditText editText, ToolLifeListEntity.ToolLifeEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setLIMITLIFE(Integer.parseInt(s.toString()));
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }


            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
